package com.lib.view.navigationbar.xuesitu;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lib.app.EventBusTagUtils;
import com.lib.base.R;
import com.lib.bean.Event;
import com.lib.fastkit.utils.share.tool.StringUtil;
import com.lib.observer.ObserverListener;
import com.lib.observer.ObserverManager;

import org.simple.eventbus.EventBus;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;


public class HomeNavigationBar extends FrameLayout implements ObserverListener {

    private View root;

    private ImageView iv_personal, iv_logo, iv_message, iv_record;

    //新版纷享圈新增
    private LinearLayout linHomeBar, linNewShare;
    private TextView tvCared, tvFind;
    private ImageView ivNotice, ivMsg;

    private TextView tv_title;

    private LinearLayout linUserVip;

    public static final int YOU_XUAN = 0;

    public static final int YOU_WEN_DA = 1;

    public static final int YOU_TU_ZI = 2;

    public static final int YOU_JIAN_KE = 3;

    public static final int YOU_FENG_XIANG = 4;


    /**
     * 有新消息的3种状态
     */
    public static final int YOU_XUAN_MSG = 5;
    public static final int YOU_TU_ZI_MSG = 6;
    public static final int YOU_FENG_XIANG_MSG = 7;

    public HomeNavigationBar(@NonNull Context context) {
        this(context, null);
    }

    public HomeNavigationBar(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HomeNavigationBar(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        ObserverManager.getInstance().add(this);

        root = inflate(context, R.layout.home_bar, this);
        initView(context, attrs);


    }

    private void initView(final Context context, @Nullable AttributeSet attrs) {
        iv_personal = root.findViewById(R.id.iv_personal);
        tv_title = root.findViewById(R.id.tv_title);
        iv_logo = root.findViewById(R.id.iv_logo);
        iv_message = root.findViewById(R.id.iv_message);

        linUserVip = root.findViewById(R.id.lin_userVip);

        //新版纷享圈新增
        linHomeBar = root.findViewById(R.id.lin_homebar);
        linNewShare = root.findViewById(R.id.lin_newshare);
        tvCared = root.findViewById(R.id.tv_newshare_cared);
        tvFind = root.findViewById(R.id.tv_newshare_find);
        ivNotice = root.findViewById(R.id.iv_newshare_notice);
        ivMsg = root.findViewById(R.id.iv_newshare_msg);

        iv_personal.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new Event<>(1, "打开个人中心"), EventBusTagUtils.HomeNavigationBar);
            }
        });

        iv_message.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new Event<>(2, "打开消息列表"), EventBusTagUtils.HomeNavigationBar);
            }
        });

        linUserVip.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new Event<>(3, "打开天使用户界面"), EventBusTagUtils.HomeNavigationBar);

                EventBus.getDefault().post(new Event<>(2, "隐藏年级学科选择"), EventBusTagUtils.ChooseFragment);
            }
        });

        //获取自定义属性
//        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ColorTrackTextView);
        tvCared.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                setTvState(1);

            }
        });

        tvFind.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                setTvState(2);
            }
        });

        ivNotice.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                EventBus.getDefault().post(new Event<>(3, "打开提醒"), EventBusTagUtils.NewShareClick);
            }
        });

        ivMsg.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                EventBus.getDefault().post(new Event<>(4, "打开消息"), EventBusTagUtils.NewShareClick);
            }
        });

    }

    /**
     * 设置顶部关注发现文字状态
     *
     * @param type 1为关注  2为发现
     */
    public void setTvState(int type) {
        if (1 == type) {

            setTvSmall(tvFind);
            setTvBig(tvCared);

            EventBus.getDefault().post(new Event<>(type, "打开关注"), EventBusTagUtils.NewShareClick);

        } else if (2 == type) {

            setTvSmall(tvCared);
            setTvBig(tvFind);

            EventBus.getDefault().post(new Event<>(type, "打开发现"), EventBusTagUtils.NewShareClick);
        }
    }

    /**
     * 将文字字体变小，并修改背景色
     *
     * @param tv
     */
    public void setTvSmall(TextView tv) {
        tv.setTextSize(16);
        tv.setTextColor(getResources().getColor(R.color.base_text1));
    }

    /**
     * 将文字字体变大，并修改背景色
     *
     * @param tv
     */
    public void setTvBig(TextView tv) {
        tv.setTextSize(20);
        tv.setTextColor(getResources().getColor(R.color.base_blue));
    }


    public void setViewType(int type) {
        iv_message.setVisibility(GONE);
        iv_logo.setVisibility(GONE);
        iv_personal.setVisibility(VISIBLE);
        linUserVip.setVisibility(GONE);

        switch (type) {
            case YOU_XUAN: {

//                tv_title.setText("课程");

                break;
            }
            case YOU_XUAN_MSG: {

//                tv_title.setText("课程");

                break;
            }

            case YOU_WEN_DA: {

                linHomeBar.setVisibility(VISIBLE);
                linNewShare.setVisibility(GONE);
                if (linHomeBar.getVisibility() == VISIBLE) {

                    tv_title.setText("精讲式答疑");
                    iv_personal.setVisibility(GONE);
                }

                break;
            }
            case YOU_TU_ZI: {
                linHomeBar.setVisibility(VISIBLE);
                linNewShare.setVisibility(GONE);

                if (linHomeBar.getVisibility() == VISIBLE) {
                    iv_logo.setVisibility(VISIBLE);
                    tv_title.setText("精讲在线答疑");
                }

                break;
            }

            case YOU_TU_ZI_MSG: {
                linHomeBar.setVisibility(VISIBLE);
                linNewShare.setVisibility(GONE);

                if (linHomeBar.getVisibility() == VISIBLE) {
                    iv_logo.setVisibility(VISIBLE);
                    tv_title.setText("精讲在线答疑");
                }
                break;
            }
            case YOU_JIAN_KE: {
                linHomeBar.setVisibility(VISIBLE);
                linNewShare.setVisibility(GONE);

                if (linHomeBar.getVisibility() == VISIBLE) {
                    tv_title.setText("尖课");
                    iv_personal.setVisibility(GONE);
                    linUserVip.setVisibility(VISIBLE);
                }

                break;
            }
            case YOU_FENG_XIANG: {
//                iv_message.setVisibility(VISIBLE);
//                iv_personal.setVisibility(GONE);
//                tv_title.setText("纷享圈");

                linHomeBar.setVisibility(GONE);
                linNewShare.setVisibility(VISIBLE);

                break;
            }
            case YOU_FENG_XIANG_MSG: {
//                iv_message.setVisibility(VISIBLE);
//                iv_personal.setVisibility(GONE);
//                tv_title.setText("纷享圈");
//                iv_message.setImageResource(R.mipmap.icon_news_new);

                linHomeBar.setVisibility(GONE);
                linNewShare.setVisibility(VISIBLE);

                ivNotice.setImageResource(R.mipmap.circle_icon_news_have);
                ivMsg.setImageResource(R.mipmap.circle_icon_personal_have);

                break;
            }
        }

    }

    /**
     * 观察者模式监听，此处不是activity中，eventbus无法使用
     * 用于监听新版纷享圈中关注界面点击查看更多切换至发现
     *
     * @param content
     */
    @Override
    public void observerUpData(String content) {

        if (StringUtil.isNotEmpty(content, true)) {
            if ("切换至发现".equals(content)) {
                setTvState(2);
            }
        }
    }
}
